﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MindFactorial.LaunchPoint.Runtime
{

    /// <summary>
    /// Represents a session on the LaunchPoint server in which multiple rules
    /// can be executed.  Use this when multiple rules must interact together 
    /// and share state amongst each other through various calls to the BRE
    /// </summary>
    public class ServerSession
    {
        Guid _session_id = Guid.Empty;
        Dictionary<string, BRERule> _rules = null;

        /// <summary>
        /// Creates a new server session object
        /// </summary>
        public ServerSession()
        {
            _session_id = Guid.NewGuid();
            _rules = new Dictionary<string, BRERule>();
        }

        /// <summary>
        /// adds a rule to the given session.  All rules
        /// within the session will be processed within the
        /// same context regardless of the adapter they belong to
        /// </summary>
        /// <param name="rule"></param>
        public void AddRuleToSession(BRERule rule)
        {
            rule.SetSession(this._session_id);
            _rules.Add(rule.Name,rule);
        }

        /// <summary>
        /// Gets a rule from the session container
        /// for execution.  the rule will run in the
        /// same server session as any other rules added to the
        /// server session object
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BRERule this[string name]
        {
            get
            {
                if(_rules.ContainsKey(name))
                    return _rules[name];

                return null;
            }
        }


    }
}
